package com.crl.module.loaders.workers
{
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;

	public class ByteSWFWorker extends Worker
	{
		private var bytesLoader:URLLoader;
		public function ByteSWFWorker()
		{
			super();
		}
		
		override public function load(request:URLRequest) : void{
			this.loader = new Loader();
			
			bytesLoader = new URLLoader();
			bytesLoader.dataFormat = URLLoaderDataFormat.BINARY;
			bytesLoader.addEventListener(Event.OPEN, onOpen);
			bytesLoader.addEventListener(ProgressEvent.PROGRESS, onProgress);
			bytesLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatus);
			bytesLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOLoadError);
			bytesLoader.addEventListener(IOErrorEvent.DISK_ERROR, onIOLoadError);
			bytesLoader.addEventListener(IOErrorEvent.NETWORK_ERROR, onIOLoadError);
			bytesLoader.addEventListener(IOErrorEvent.VERIFY_ERROR, onIOLoadError);
			bytesLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
			bytesLoader.addEventListener(Event.COMPLETE, onCompleteLocal);
		}
		private function onCompleteLocal(event:Event):void{
			loader.contentLoaderInfo.addEventListener(Event.INIT, onBytesInitialized)
			loader.loadBytes(bytesLoader.data as ByteArray);
		}
		
		private function onBytesInitialized(e:Event):void
		{
			var loadInfo:LoaderInfo=e.target as LoaderInfo;
			loadInfo.removeEventListener(Event.INIT, onBytesInitialized);
			bytesLoader.close();
			bytesLoader = null;
			
			_data=loadInfo;
			super.onComplete(e);
		}
	}
}